package com.dqhl.smartstable.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;


import com.dqhl.smartstable.R;
import com.dqhl.smartstable.base.BaseActivity;
import com.dqhl.smartstable.util.Config;
import com.dqhl.smartstable.util.Constant;
import com.dqhl.smartstable.util.Dlog;
import com.dqhl.smartstable.util.JsonUtils;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import cn.jpush.android.api.JPushInterface;

/**
 * 显示接收到的推送消息的activity
 */
public class ShowJPushMsgActivity extends BaseActivity implements View.OnClickListener {

    private ImageView iv_top_back;
    private TextView tv_top_center;
    private TextView tv_result;
    private TextView tv_warning;
//    private Button btn_stop;
    private String token; //用户token
    private String strSN; //箱子号SN

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_msg);

        initView();
        showResult();

    }

    private void showResult() {
        tv_result.setText("用户自定义打开的Activity");
        Bundle bundle = getIntent().getExtras();
        if (null != bundle) {
            String title = bundle.getString(JPushInterface.EXTRA_NOTIFICATION_TITLE);
            String content = bundle.getString(JPushInterface.EXTRA_ALERT);
            tv_result.setText("Title : " + title + "  " + "Content : " + content);
            if (null != content) {
                if (content.equals("2")) {
                    tv_warning.setText("您的箱子有异常，请及时处理");
                } else if (content.equals("4")){
                    tv_warning.setText("您的箱子有异常，请及时处理");
                }
            }
        }
    }

    private void initView() {
        iv_top_back = (ImageView) findViewById(R.id.iv_top_back);
        tv_top_center = (TextView) findViewById(R.id.tv_top_center);
        tv_top_center.setText("报警信息");
        tv_result = (TextView) findViewById(R.id.tv_result);
        tv_warning = (TextView) findViewById(R.id.tv_warning);
//        btn_stop = (Button) findViewById(R.id.btn_stop);

        iv_top_back.setOnClickListener(this);
//        btn_stop.setOnClickListener(this);

        token = sp.getString(Constant.TOKEN, "");
        strSN = sp.getString(Constant.SN, "");
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
//            case R.id.btn_stop: //取消监控
//                doCancelAlarm();
//                break;
            case R.id.iv_top_back:
                finish();
                break;
        }
    }


    /**
     * 取消警报
     */
    private void doCancelAlarm() {
        Dlog.e("token:" + token + ";case_sign:" + strSN);
        RequestParams params = new RequestParams(Config.Api.cancel_alarm);
        params.addBodyParameter("case_sign", strSN);
        params.addBodyParameter("token", token);
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Dlog.e(result);
                String msg = JsonUtils.getMsg(result);
                String code = JsonUtils.getCode(result);
                toast(code);
                Dlog.e(msg);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

}
